import {messages} from "share/common";
import React from "react";
import WrapFormItem from "components/wrap-form-item";
import { Row, Col } from "antd";
import Chooser from 'components/chooser'

const hasValue = value => value && value.costCenterId;

const onChangeType = (innerValue, onChange) => {
  onChange({
    costCenterId: innerValue,
    costCenterItemId: []
  });
};

export default {
  id: 'costCenterItemId', label: messages('basic-5.key648')/*成本中心项*/,
  dataIndex: 'costCenterItemId',
  component: <WrapFormItem parser={value => value || {}}>
    {(onChange, value) => (
      <Row gutter={10}>
        <Col span={12}>
          <Chooser
            type='select_cost_center_by_sobid'
            single
            placeholder={messages('basic-5.key671')/*请选择成本中心*/}
            labelKey='name'
            valueKey='id'
            value={value.costCenterId}
            onChange={innerValue => onChangeType(innerValue, onChange)}
          />
        </Col>
        <Col span={12} style={{ height: 32 }}>
          <Chooser
            type='cost_center_item_by_id_new'
            single
            placeholder={messages('basic-5.key574')/*请选择成本中心项*/}
            disabled={ !value.costCenterId }
            valueKey='id'
            listExtraParams={{ costCenterId: value.costCenterId && value.costCenterId[0] && value.costCenterId[0].id }}
            labelKey='name'
            onChange={innerValue => onChange({ costCenterItemId: innerValue })}
            value={value.costCenterItemId}
          />
        </Col>
      </Row>
    )}
  </WrapFormItem>,
  hasValue,
  parser: value => hasValue(value) ? {
    costCenterItemId: value.costCenterItemId && value.costCenterItemId[0] && value.costCenterItemId[0].id
  } : null
}
